import WxValidate from '../../utils/WxValidate'
const util = require('../../utils/util.js')
Page({
    /**
     * 页面的初始数据
     */
    data: {

        inputValue: '',
        inputPosition: '',
        scanDate: '',
    },

    //同步工号
    bindKeyInput: function (e) {

        this.setData({
            inputValue: e.detail.value

        })
    },
    //获取扫描数据
    scan_position: function (e) {

        var that = this
        wx.scanCode({
            success(res) {
                //   console.log(res);
                that.setData({
                    inputPosition: res.result
                })
                wx.showToast({
                    icon: 'success',
                    duration: 1000
                })
            },
            fail: (res) => {
                console.log(res);
                wx.showToast({
                    title: '扫描失败',
                    icon: 'none',
                    duration: 1000
                })
            }
        })
    },
    //验证函数错误提示
    showModal(error) {
        wx.showModal({
            content: error.msg,
            showCancel: false,
        })
    },
    //验证函数
    initValidate() {
        const rules = {
            inputValue: {
                required: true,
                number: true,
                minlength: 5,
                maxlength: 7

            },
            inputPosition: {
                required: true,
            }
        }
        const messages = {
            inputValue: {
                required: '请输入工号',
                minlength: '请输入正确工号'
            },
            inputPosition: {
                required: '请扫描地址二维码',
            }
        }
        this.WxValidate = new WxValidate(rules, messages)
    },
    // 进入 提交按钮的bindtap //  
    //提交数据 并且 调用验证函数
    enter_sub: function () {
        console.log('进入事件')
        const params = {
            inputValue: this.data.inputValue,
            inputPosition: this.data.inputPosition
        }
        //校验表单
        //校验表单
        if (!this.WxValidate.checkForm(params)) {
            const error = this.WxValidate.errorList[0]
            this.showModal(error)
            return false
        }
        this.showModal({
            msg: '提交成功'
        })

        //以下为写入数据库部分
        console.log('data_inputValue',)
        const inputValue= this.data.inputValue
        const inputPosition= this.data.inputPosition
        const ui = wx.getStorageSync("userInfo")
        //通过云函数把数据写入到logs集合内
        console.log('data_inputValue',inputValue)
        wx.cloud.callFunction({
            //云函数名字
            name: "createlog",
            data: {
                openid:ui.openid,
                gh: inputValue,
                dd: inputPosition,
                fs: '进入',
                date: Date.now(),
                wxm: ui.nickName
            }
        }).then(res=>{
            console.log("传参成功，return值：", res.result)
        }).catch(err=>{
            console.log('catch err',err.result)
        })
        //初始化
        wx.cloud.init()
        //获取数据库
        const db = wx.cloud.database()
        //获取集合
        const trail_data = db.collection('trail_data')
        //添加数据到集合
        trail_data.add({
            data: {
                gh: this.data.inputValue,
                dd: this.data.inputPosition,
                sj: this.data.scanDate,
                fs: '进入'
            }
        }).then(res => console.log(res)).catch(err => console.log(err))

        this.setData({
            inputPosition: ''
        })
    },
    // 离开 提交按钮的bindtap //  
    //提交数据 并且 调用验证函数
    exit_sub: function () {
        console.log('离开事件')
        const params = {
            inputValue: this.data.inputValue,
            inputPosition: this.data.inputPosition
        }

        //校验表单
        if (!this.WxValidate.checkForm(params)) {
            const error = this.WxValidate.errorList[0]
            this.showModal(error)
            return false
        }
        this.showModal({
            msg: '提交成功'
        })

        //以下为写入数据库部分
        console.log('data_inputValue',)
        const inputValue= this.data.inputValue
        const inputPosition= this.data.inputPosition
        const ui = wx.getStorageSync("userInfo")
        //通过云函数把数据写入到logs集合内
        console.log('data_inputValue',inputValue)
        wx.cloud.callFunction({
            //云函数名字
            name: "createlog",
            data: {
                openid:ui.openid,
                gh: inputValue,
                dd: inputPosition,
                fs: '离开',
                date: Date.now(),
                wxm: ui.nickName
            }
        }).then(res=>{
            console.log("传参成功，return值：", res.result)
        }).catch(err=>{
            console.log('catch err',err.result)
        })
        //初始化
        wx.cloud.init()
        //获取数据库
        const db = wx.cloud.database()
        //获取集合
        const trail_data = db.collection('trail_data')
        //添加数据到集合
        trail_data.add({
            data: {
                gh: this.data.inputValue,
                dd: this.data.inputPosition,
                sj: this.data.scanDate,
                fs: '离开'
            }
        }).then(res => console.log(res)).catch(err => console.log(err))

        this.setData({
            inputPosition: ''
        })
    },
    //加载函数 调用验证规则函数、设置时间、验证微信登录、设置微信昵称
    onLoad: function () {
        this.initValidate() //验证规则函数
        this.setData({
            scanDate: util.formatTime(new Date())
        })
        const ui = wx.getStorageSync("userInfo")
        if(Object.keys(ui).length === 0){
        // if (!ui.nickName) {
            wx.switchTab({
                url: '/pages/me/me',
            })
        }
    },
    //切换页面清空二维码
    onShow(){
        const ui = wx.getStorageSync("userInfo")
        if(Object.keys(ui).length === 0){
        // if (!ui.nickName) {
            wx.switchTab({
                url: '/pages/me/me',
            })
        }
        this.setData({
            inputPosition: ''
        })
    },
    //视频里的函数
    // addLog(event) {
    //     const add = event.currentTarget.dataset.add
    //     console.log("add", add)
    //     const ui = wx.getStorageSync("userInfo")
    //     console.log('userInfo对象', ui)
    //     if (!ui.openid) {
    //         wx.switchTab({
    //             url: '/pages/me/me',
    //         })
    //     } else {
    //         wx.cloud.callFunction({
    //             name: "createlog",
    //             data: {
    //                 add: add,
    //                 date: Date.now(),
    //                 openid: ui.openid
    //             }
    //         })
    //     }

    // }
})